class LipStick
{

}

class Mirror
{

}

//class MakeUp extends Thread
//{
//    private int flag;  //如果是0，拿的是口红，如果是1，拿的是镜子
//    private String girlName;
//
//    static LipStick lipStick = new LipStick();
//    static Mirror mirror = new Mirror();
//
//    public MakeUp (int flag, String girlName)
//    {
//        this.flag = flag;
//        this.girlName = girlName;
//    }
//
//    @Override
//    public void run ()
//    {
//        try
//        {
//            this.doMakeUp();
//        }
//        catch(InterruptedException e)
//        {
//            e.printStackTrace();
//        }
//    }
//
//    public void doMakeUp() throws InterruptedException
//    {
//        if(flag == 0)
//            synchronized(lipStick)
//            {
//                System.out.println(girlName + "拿着口红");
//                Thread.sleep(1000);
//
//                synchronized(mirror)
//                {
//                    System.out.println(girlName + "拿着镜子");
//                    Thread.sleep(1000);
//                }
//            }
//        else
//            synchronized(mirror)
//            {
//                System.out.println(girlName + "拿着镜子");
//                Thread.sleep(1000);
//
//                synchronized(lipStick)
//                {
//                    System.out.println(girlName + "拿着口红");
//                    Thread.sleep(1000);
//                }
//            }
//
//    }
//}

//解决
class MakeUp extends Thread
{
    private int flag;  //如果是0，拿的是口红，如果是1，拿的是镜子
    private String girlName;
    
    static LipStick lipStick = new LipStick();
    static Mirror mirror = new Mirror();
    
    public MakeUp (int flag, String girlName)
    {
        this.flag = flag;
        this.girlName = girlName;
    }
    
    @Override
    public void run ()
    {
        try
        {
            this.doMakeUp();
        }
        catch(InterruptedException e)
        {
            e.printStackTrace();
        }
    }
    
    public void doMakeUp() throws InterruptedException
    {
        if(flag == 0)
        {
            synchronized(lipStick)
            {
                System.out.println(girlName + "拿着口红");
                Thread.sleep(1000);
            }
    
            synchronized(mirror)
            {
                System.out.println(girlName + "拿着镜子");
                Thread.sleep(1000);
            }
        }
        else
        {
            synchronized(mirror)
            {
                System.out.println(girlName + "拿着镜子");
                Thread.sleep(1000);
            }
    
            synchronized(lipStick)
            {
                System.out.println(girlName + "拿着口红");
                Thread.sleep(1000);
            }
        }
        
    }
}

public class TestDeath
{
    public static void main (String[] args)
    {
        new MakeUp(0, "小丫").start();
        new MakeUp(1, "大丫").start();
    }
}
